package com.yawn.zerodefect.config;

import com.yawn.zerodefect.controller.interceptor.IndexInterceptor;
import com.yawn.zerodefect.controller.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

import javax.annotation.Resource;

/**
 * @author Created by yawn on 2018-01-26 11:06
 */
@Configuration
@EnableWebMvc
public class MvcConfig extends WebMvcConfigurerAdapter{

    @Resource
    private LoginInterceptor loginInterceptor;
    @Resource
    private IndexInterceptor indexInterceptor;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/templates/**").addResourceLocations("classpath:/templates/");
        super.addResourceHandlers(registry);
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/user");
        registry.addViewController("/project");
        registry.addViewController("/defect");
        registry.addViewController("/about");
        registry.addViewController("/default");
        registry.addRedirectViewController("/","/default");
        super.addViewControllers(registry);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 基于 WebRequest 的拦截器
        registry.addInterceptor(loginInterceptor);
        // 基于 HttpServletRequest 的拦截器
        registry.addInterceptor(indexInterceptor);
        super.addInterceptors(registry);
    }
}
